#-------------------------------------------------------------------------------
# Name:        module1
# Purpose:
#
# Author:      Rafael, Daniel
#
# Created:     06/05/2011
# Copyright:   (c) Laboratorio de Compositos 2011
# Licence:     GNU GPL v3.0
#-------------------------------------------------------------------------------
#!/usr/bin/env python

import random

def main():
    pass

if __name__ == '__main__':
    main()

population = []

# Defines Lamina Class
# layup =  array contaning stack sequence
# fitness = double containing fitness value
# weight = weight of Lamina in population, relative to its fitness value
class Lamina:
    def __init__(self, layup, fitness, weight):
        self.layup = layup
        self.fitness = fitness
        self.weight = weight

# Calculates the fitness value of a given layup
# Eg.: Laminate Strength
def fitness(layup):
    fit = 0
    for n in layup:
        fit = fit + n
    return fit

# Calculates the weight of a given layup
def weight(population, layup):
    total = 0
    for n in population:
        total = total + n.fitness
    for n in population:
        n.weight = float(n.fitness) / total


n = 0
while n < 10:
    m = 0
    layup = []
    while m < 4:
        layup.append(random.randint(0,88))
        m = m + 1
    population.append(Lamina(layup, fitness(layup), 0.0))
    n = n + 1

# Calls weight() for each layup in population
for layup in population:
    weight(population, layup)
    print layup.layup, "Fitness:", layup.fitness, "Weight:", layup.weight

# Crossover two given layups at a random point
def Crossover(layup1, layup2):
    layup3 = [0,0,0,0]
    layup4 = [0,0,0,0]
    point = random.randint(1,len(layup1)-1)
    n = 0
    while n < 4:
        if n < point:
            layup3[n] = layup1[n]
            layup4[n] = layup2[n]
        else:
            layup3[n] = layup2[n]
            layup4[n] = layup1[n]
        n = n + 1
    print "Point:" , point, "  Layups:", layup3, layup4